package com.wuhongyu.service.impl;

import com.wuhongyu.base.BaseServiceImpl;
import com.wuhongyu.common.MyCommon;
import com.wuhongyu.entity.Customer;
import com.wuhongyu.entity.Orders;
import com.wuhongyu.entity.Runoff;
import com.wuhongyu.entity.User;
import com.wuhongyu.repository.CustomerDao;
import com.wuhongyu.repository.OrdersDao;
import com.wuhongyu.repository.RunoffDao;
import com.wuhongyu.repository.UserDao;
import com.wuhongyu.base.BaseService;
import com.wuhongyu.service.CustomerService;
import com.wuhongyu.util.MyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service("customerService")
public class CustomServiceImpl extends BaseServiceImpl<Customer> implements CustomerService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private OrdersDao ordersDao;
    @Autowired
    private RunoffDao runoffDao;

    private CustomerDao customerDao;
    @Resource
    public void setDao(CustomerDao customerDao){
        super.setBaseDao(customerDao);
        this.customerDao = customerDao;
    }
    @Override
    public Page<Customer> findAll(Customer customer, Integer number) {
        Specification<Customer> spec = (root, query, cb) -> {
            Join<Customer, User> root1 = root.join("managerUser");
            List<Predicate> list = new ArrayList<>();
            if(customer.getIdentity()!=null)
                list.add(cb.like(root.get("identity"),"%"+customer.getIdentity()+"%"));
            if(customer.getName()!=null)
                list.add(cb.like(root.get("name"),"%"+customer.getName()+"%"));
            if(customer.getArea()!=null)
                list.add(cb.like(root.get("area"),"%"+customer.getArea()+"%"));
            if(customer.getManagerUser()!=null)
                list.add(cb.like(root1.get("name"),"%"+customer.getManagerUser().getName()+"%"));
            if(customer.getGrade()!=null)
                list.add(cb.like(root.get("grade"),"%"+customer.getGrade()+"%"));
            //查找的时候找的是状态为0的客户
            list.add(cb.equal(root.get("status"),0));
            return cb.and(list.toArray(new Predicate[list.size()]));
        };
        return customerDao.findAll(spec, new PageRequest(MyUtil.getCurrentPage(number), MyCommon.PAGESIZE));
    }

    @Override
    public void save(Customer customer) {
        customer.setManagerUser(userDao.getOne(customer.getManagerUser().getId()));
        if(customer.getIdentity()==null)
            customer.setIdentity("WHY-"+UUID.randomUUID().toString());
        //状态0为正常客户
        customer.setStatus(0);
        customerDao.saveAndFlush(customer);
    }

    //删除功能就是客户流失
    @Override
    public void delete(Integer id) {
        Customer customer = customerDao.getOne(id);
        //状态1为流失客户
        customer.setStatus(1);
        customerDao.saveAndFlush(customer);
        //这里还需要创建客户流失表...
        //....
        //为客户流失表初始化
        Runoff runoff = new Runoff();
        runoff.setCustomer(customer);
        runoff.setStatus(0);
        List<Orders> ordersList = ordersDao.findByCustomerEqualsOrderByCreatetime(customer);
        if(ordersList.size()>0){
            runoff.setLasttime(ordersList.get(ordersList.size()-1).getCreatetime());
        }
        runoffDao.saveAndFlush(runoff);
    }

}